package com.dc.web.controller;


import com.dc.dao.CalibrationAirPowerDao;
import com.dc.entity.CalibrationAirPowerEntity;
import com.dc.entity.UserEntity;
import com.dc.model.AjaxResult;
import com.dc.model.PageListBody;
import com.dc.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Optional;

/**
 三级界面-压缩空气气电转换标定统计表
 *
 */

@RestController
@RequestMapping("/api/calibration_air_power")
@Slf4j
public class CalibrationAirPowerController {

    @Resource
    CalibrationAirPowerDao calibrationAirPowerDao;

    @Resource
    UserService userService;

    @GetMapping("/list")
    public AjaxResult list(PageListBody body) {

        Integer pageNumber = body.getPageNumber();
        pageNumber = ObjectUtils.defaultIfNull(pageNumber, 1);
        Integer pageSize = body.getPageSize();
        pageSize = ObjectUtils.defaultIfNull(pageSize, 20);

        PageRequest pageRequest = PageRequest.of(pageNumber - 1, pageSize);

        Page<CalibrationAirPowerEntity> all = calibrationAirPowerDao.findAll(pageRequest);
        return AjaxResult.success(all);
    }

    @GetMapping("/{id}")
    public AjaxResult show(@PathVariable("id") Long id) {

        CalibrationAirPowerEntity entity = null;

        Optional<CalibrationAirPowerEntity> optional = calibrationAirPowerDao.findById(id);
        if(optional.isPresent()) {
            entity = optional.get();
        }

        return AjaxResult.success(entity);
    }

    @PostMapping()
    public AjaxResult add(@RequestHeader HttpHeaders httpHeaders, @RequestBody CalibrationAirPowerEntity entity) {

        UserEntity user = userService.getUser(httpHeaders);
        entity.setId(null);
        entity.setUpdater(user.getUserName());

        // 动态计算
        calc(entity);

        CalibrationAirPowerEntity save = calibrationAirPowerDao.save(entity);

        return AjaxResult.success(save);
    }

    @PutMapping("/{id}")
    public AjaxResult update(@RequestHeader HttpHeaders httpHeaders,
                             @PathVariable("id") Long id,
                             @RequestBody CalibrationAirPowerEntity entity) {
        UserEntity user = userService.getUser(httpHeaders);
        entity.setId(id);
        entity.setUpdater(user.getUserName());

        // 动态计算
        calc(entity);

        CalibrationAirPowerEntity save = calibrationAirPowerDao.save(entity);

        return AjaxResult.success(save);
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {

        calibrationAirPowerDao.deleteById(id);

        return AjaxResult.success();
    }

    private CalibrationAirPowerEntity calc(CalibrationAirPowerEntity entity) {
        // inAir = outAir * timespan
        try {
            entity.inAir = entity.outAir * entity.timespan;
        } catch (Exception e) {
            entity.inAir = 0.;
        }

        // powerByUnit = power / inAir
        try {
            entity.powerByUnit = entity.power / entity.inAir;
        } catch (Exception e) {
            entity.powerByUnit = 0.;
        }

        return entity;
    }
}
